package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.util.ApplicationContextProvider;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.util.Message;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Action;
import com.lb.jwitter.service.TblCTDTKhoiNganh;
import com.lb.jwitter.service.TblCTDTKhoiNganhService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblCTDTMonHocTienQuyet;
import com.lb.jwitter.service.TblCTDTMonHocTienQuyetService;
import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;

/**
 * @author QUOCVIET
 * 
 */

@ContextConfiguration(locations = "classpath:beans.xml")
public class TblCTDTMonHocTienQuyetInsertAction {
	private int pageSize;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	private int page;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	private int pages;

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	private String dataString;

	public String getDataString() {
		return dataString;
	}

	private Message msg;

	public Message getMsg() {
		return msg;
	}

	private Long id;

	public void setId(Long id) {
		this.id = id;
	}

	private String ma;

	public void setMa(String ma) {
		this.ma = ma;
	}

	public String getMa() {
		return ma;
	}

	// id mon hoc1
	private Long monHoc1Id;

	public void setMonHoc1Id(Long monHoc1Id) {
		this.monHoc1Id = monHoc1Id;
	}

	public Long getMonHoc1Id() {
		return monHoc1Id;
	}
	
	//id mon hoc 2
	private Long monHoc2Id;

	public void setMonHoc2Id(Long monHoc2Id) {
		this.monHoc2Id = monHoc2Id;
	}

	public Long getMonHoc2Id() {
		return monHoc2Id;
	}

	//ma mon hoc 1
	private String maMonHoc1;

	public void setMaMonHoc1(String mamh) {
		this.maMonHoc1 = mamh;
	}

	public String getMaMonHoc1() {
		return maMonHoc1;
	}

	//ma mon hoc 2
	private String maMonHoc2;

	public void setMaMonHoc2(String mamh) {
		this.maMonHoc2 = mamh;
	}

	public String getMaMonHoc2() {
		return maMonHoc2;
	}
	
	private Long khoiNganhId;

	public void setKhoiNganhId(Long khoiNganhId) {
		this.khoiNganhId = khoiNganhId;
	}

	public Long getKhoiNganhId() {
		return khoiNganhId;
	}
	
	private String maKhoiNganh; 
    public void setMaKhoiNganh(String ma) {
       this.maKhoiNganh = ma;
  }

    public String getMaKhoiNganh() {
       return maKhoiNganh;
  }

	private TblCTDTMonHocService tblCTDTMonHocService;

	private TblCTDTKhoiNganhService tblCTDTKhoiNganhService;

	private TblCTDTMonHocTienQuyet tblCTDTMonHocTienQuyet;

	public TblCTDTMonHocTienQuyet getTblCTDTMonHocTienQuyet() {
		return tblCTDTMonHocTienQuyet;
	}

	private TblCTDTMonHocTienQuyetService tblCTDTMonHocTienQuyetService;

	private int preInsert(Message msg, TblCTDTMonHocTienQuyet obj) {
		msg.setPreMessage_b(true);
		msg.setPreMessage_s("success");
		return 1;
	}

	private int postInsert(Message msg, TblCTDTMonHocTienQuyet obj) {
		msg.setPostMessage_b(true);
		msg.setPostMessage_s("success");
		return 1;
	}

	private TblTaiKhoan taiKhoanDangNhap;

	public TblTaiKhoan getTaiKhoanDangNhap() {
		return taiKhoanDangNhap;
	}

	public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) {
		this.taiKhoanDangNhap = taiKhoanDangNhap;
	}

	private TblQuyenChiTietService tblQuyenChiTietService;
	@Autowired
	// Injected by Spring
	ApplicationContextProvider contextProvider;

	public String execute() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap
					.getMaNhomQuyen().getId(), "tbl_CTDT_MonHoc_TienQuyet",
					"them")) {
				return "noprivilege";
			}
			tblCTDTMonHocTienQuyetService = (TblCTDTMonHocTienQuyetService) context
					.getBean("tbl_CTDT_MonHoc_TienQuyetClient");
			tblCTDTMonHocService = (TblCTDTMonHocService) context
					.getBean("tbl_CTDT_MonHocClient");

			msg = new Message();
			// -------------------------------------
			// tim id cho mso mon hoc
			List<ConditionReport> cons = new ArrayList<ConditionReport>();
			{
				ConditionReport con = new ConditionReport();
				con.setCol("ma");
				con.setValue1(maMonHoc1);
				con.setOperator("eq");
				cons.add(con);
			}

			List<TblCTDTMonHoc> monhocs = tblCTDTMonHocService.findtblCTDTMonHocByCondition(cons, 0, 0);
			if (monhocs != null && monhocs.size() > 0) {
				monHoc1Id = monhocs.get(0).getId();
			} else {
				return Action.ERROR;
			}
			
			//-------------
			cons = new ArrayList<ConditionReport>();
			{
				ConditionReport con = new ConditionReport();
				con.setCol("ma");
				con.setValue1(maMonHoc2);
				con.setOperator("eq");
				cons.add(con);
			}

			monhocs = tblCTDTMonHocService.findtblCTDTMonHocByCondition(cons, 0, 0);
			if (monhocs != null && monhocs.size() > 0) {
				monHoc2Id = monhocs.get(0).getId();
			} else {
				return Action.ERROR;
			}
			
			//-------------
			cons = new ArrayList<ConditionReport>();
			{
				ConditionReport con = new ConditionReport();
				con.setCol("ma");
				con.setValue1(maKhoiNganh);
				con.setOperator("eq");
				cons.add(con);
			}

			List<TblCTDTKhoiNganh> khoinganhs = tblCTDTKhoiNganhService.findtblCTDTKhoiNganhByCondition(cons, 0, 0);
			if (khoinganhs != null && khoinganhs.size() > 0) {
				khoiNganhId = khoinganhs.get(0).getId();
			} else {
				return Action.ERROR;
			}
			

			// tao doi tuong moi
			tblCTDTMonHocTienQuyet = new TblCTDTMonHocTienQuyet();
			tblCTDTMonHocTienQuyet.setMa(ma);
			tblCTDTMonHocTienQuyet.setMonHoc1Id(tblCTDTMonHocService
					.gettblCTDTMonHoc(new Long(monHoc1Id)));
			tblCTDTMonHocTienQuyet.setMonHoc2Id(tblCTDTMonHocService
					.gettblCTDTMonHoc(new Long(monHoc2Id)));
			tblCTDTMonHocTienQuyet.setKhoiNganhId(tblCTDTKhoiNganhService.gettblCTDTKhoiNganh(khoiNganhId));
			
			preInsert(msg, tblCTDTMonHocTienQuyet);
			if (msg.isPreMessage_b()) {
				id = tblCTDTMonHocTienQuyetService
						.createtblCTDTMonHocTienQuyet(tblCTDTMonHocTienQuyet);
				postInsert(msg, tblCTDTMonHocTienQuyet);
				if (msg.isPostMessage_b() == false) {
					return Action.ERROR;
				}
			} else {
				return Action.ERROR;
			}
			tblCTDTMonHocTienQuyet.setId(id);
			if (pageSize == 0) {
				pageSize = 20;
			}
			if (page == 0) {
				page = 1;
			}
			dataString = "";
			dataString += "&id=";
			dataString += "&ma=";
			dataString += "&monHoc1Id=";
			dataString += "&monHoc2Id=";
			return Action.SUCCESS;
		} catch (Exception e) {
			System.out.println("Loi Insert tbl_CTDT_MonHoc_TienQuyet Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}
}
